package com.czy.owner.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.UserCollectInfoModel;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.utils.glide.GlideUtils;
import java.util.List;


/**
 * Created by wangyang on 2017/5/14 0014.
 */

public class UserCollectInfoAdapter extends BaseRecyclerAdapter<UserCollectInfoModel.ListBean, UserCollectInfoAdapter.UserColectInfoHolder> {
    private Context context;
    private DeletedListener deleteListener;
    public UserCollectInfoAdapter(Context context, List<UserCollectInfoModel.ListBean> data, boolean slideEnabled, boolean reboundEnabled) {
        super(context, data, slideEnabled, reboundEnabled);
        this.context = context;
    }

    @Override
    public UserColectInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_user_collect_info, parent, false);
        return new UserColectInfoHolder(view);
    }

    @Override
    public void onBindViewHolder(final UserColectInfoHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
        holder.tvItemDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteListener != null) {
                    deleteListener.onClick(view, position,data);
                }
            }
        });


    }

    public class UserColectInfoHolder extends BaseRecyclerAdapter.ViewHolder {
        private ImageView imgFindCarInformation;
        private TextView tvFindCarInfromationTitle;
        private TextView tvFindCarInfromationTime;
        private TextView tvFindCarInfromationRead;
        private LinearLayout slideButtonLayout;
        private TextView tvItemDeleteBtn;

        public UserColectInfoHolder(View itemView) {
            super(itemView);
            imgFindCarInformation = (ImageView) itemView.findViewById(R.id.img_find_car_information);
            tvFindCarInfromationTitle = (TextView) itemView.findViewById(R.id.tv_find_car_infromation_title);
            tvFindCarInfromationTime = (TextView) itemView.findViewById(R.id.tv_find_car_infromation_time);
            tvFindCarInfromationRead = (TextView) itemView.findViewById(R.id.tv_find_car_infromation_read);
            slideButtonLayout = (LinearLayout) itemView.findViewById(R.id.slide_button_layout);
            tvItemDeleteBtn = (TextView) itemView.findViewById(R.id.tv_item_delete_btn);

        }

        public void bindData(UserCollectInfoModel.ListBean data) {
            slideButtonLayout.setVisibility(View.VISIBLE);
            tvFindCarInfromationTitle.setText(data.getTitle());
            tvFindCarInfromationTime.setText(TimeUtils.getTimeRange(TimeUtils.formatTimeDifference(data.getCreateTime())));
            tvFindCarInfromationRead.setText(data.getViewCount() + "阅读");
            GlideUtils.loadImage(context, data.getCoverUrl(), imgFindCarInformation, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_banner, R.mipmap.default_banner);

        }
    }

    public interface DeletedListener {
        void onClick(View v, int position,List<UserCollectInfoModel.ListBean> data);
    }

    public void setDeleteListener(DeletedListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public void removeData(int position) {
        data.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, data.size() - position);
    }



}